package bert;

public class Vec2 {

	public final int X;
	public final int Y;
	
	public Vec2(int x, int y)
	{
	  this.X = x;
	  this.Y = y;
	}
	
	public Vec2 add(Vec2 other)
	{
		return new Vec2(this.X + other.X, this.Y + other.Y);		
	}
	
	public Vec2 substract(Vec2 other) 
	{
		return new Vec2(this.X - other.X, this.Y - other.Y);
	}
	
	public float length() 
	{
		return (float) Math.sqrt(this.X * this.X + this.Y * this.Y);
	}
	  
	public float distance(Vec2 other) {
		return other.substract(this).length();
	} 
	  
    public boolean isEquivalentTo(Vec2 other) 
    {
    	return this.X == other.X && this.Y == other.Y;
	}
    
    public Vec2 Perp1() {
    	return new Vec2(-this.Y, this.X);
    }
    
    public Vec2 Perp2() {
    	return new Vec2(this.Y, -this.X);
    }

    public Vec2 Resized(float newLength)
    {
    	float oldLength = this.length();
    	int newX = (int)((this.X * newLength) / oldLength); 
    	int newY = (int)((this.Y * newLength) / oldLength);
    	
    	return new Vec2(newX, newY);
    }
    
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + X;
		result = prime * result + Y;
		return result;
	}  

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Vec2 other = (Vec2) obj;
        
        return this.isEquivalentTo(other);
    }
}
